//
//  NSDate+Extension.m
//  百思不得姐
//
//  Created by 胥鸿儒 on 16/6/15.
//  Copyright © 2016年 xuhongru. All rights reserved.
//

#import "NSDate+Extension.h"

@implementation NSDate (Extension)

- (NSDateComponents *)deltaFrom:(NSDate *)fromDate
{
    //日期比较
    return [[NSCalendar currentCalendar]components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:fromDate toDate:self options:0];
}

- (BOOL)isThisYear
{
    NSDateComponents *currentComponents = [[NSCalendar currentCalendar]components:NSCalendarUnitYear fromDate:[NSDate date]];
    
    NSDateComponents *selfComponents = [[NSCalendar currentCalendar]components:NSCalendarUnitYear fromDate:self];
    
    return currentComponents.year == selfComponents.year;
}


- (BOOL)isToday
{
    if (!self.isThisYear)return NO;

    
   NSDateComponents *currentComponents = [[NSCalendar currentCalendar]components:NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
    
   NSDateComponents *selfComponents = [[NSCalendar currentCalendar]components:NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
    
   return currentComponents.month == selfComponents.month
    && currentComponents.day == selfComponents.day;
}
- (BOOL)isYesterday
{
    if (!self.isThisYear) return NO;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"MM-dd";
    
    NSDate *currentDate = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
    
    NSDate *selfDate = [formatter dateFromString:[formatter stringFromDate:self]];
    
    NSDateComponents *cmps = [[NSCalendar currentCalendar]components:NSCalendarUnitMonth | NSCalendarUnitDay fromDate:selfDate toDate:currentDate options:0];
    
    return !cmps.month && cmps.day == 1;

}


@end
